var t = function() {
    return new Promise(function(t) {
        return setTimeout(t, 20);
    });
};

(0, require("../common/component").VantComponent)({
    classes: [ "title-class", "content-class" ],
    relation: {
        name: "collapse",
        type: "ancestor",
        linked: function(collapseComponent) {
            this.parent = collapseComponent;
        }
    },
    props: {
        name: null,
        title: null,
        value: null,
        icon: String,
        label: String,
        disabled: Boolean,
        clickable: Boolean,
        border: {
            type: Boolean,
            value: !0
        },
        isLink: {
            type: Boolean,
            value: !0
        }
    },
    data: {
        contentHeight: 0,
        expanded: !1,
        transition: !1
    },
    mounted: function() {
        var n = this;
        this.updateExpanded().then(t).then(function() {
            n.set({
                transition: !0
            });
        });
    },
    methods: {
        updateExpanded: function() {
            if (!this.parent) return Promise.resolve();
            var parentData = this.parent.data, selectedValue = parentData.value, isAccordion = parentData.accordion, children = this.parent.children, childComponents = void 0 === children ? [] : children, itemName = this.data.name, itemIndex = childComponents.indexOf(this), itemIdentifier = null == itemName ? itemIndex : itemName, isExpanded = isAccordion ? selectedValue === itemIdentifier : (selectedValue || []).some(function(value) {
                return value === itemIdentifier;
            }), actions = [];
            return isExpanded !== this.data.expanded && actions.push(this.updateStyle(isExpanded)), actions.push(this.set({
                index: itemIndex,
                expanded: isExpanded
            })), Promise.all(actions);
        },
        updateStyle: function(isExpanded) {
            var collapseItemComponent = this;
            return this.getRect(".van-collapse-item__content").then(function(rect) {
                return rect.height;
            }).then(function(contentHeight) {
                return isExpanded ? collapseItemComponent.set({
                    contentHeight: contentHeight ? contentHeight + "px" : "auto"
                }) : collapseItemComponent.set({
                    contentHeight: contentHeight + "px"
                }).then(t).then(function() {
                    return collapseItemComponent.set({
                        contentHeight: 0
                    });
                });
            });
        },
        onClick: function() {
            if (!this.data.disabled) {
                var data = this.data, itemName = data.name, isExpanded = data.expanded, itemIndex = this.parent.children.indexOf(this), itemIdentifier = null == itemName ? itemIndex : itemName;
                this.parent.switch(itemIdentifier, !isExpanded);
            }
        },
        onTransitionEnd: function() {
            this.data.expanded && this.set({
                contentHeight: "auto"
            });
        }
    }
});